package c.x.jy.service.service.impl;

import c.x.jy.common.exception.RRException;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;
import c.x.jy.dao.dao.TeacherServiceDao;
import c.x.jy.dao.dto.ServiceEntityDto;
import c.x.jy.dao.dto.TeacherServiceEntityDto;
import c.x.jy.dao.entity.TeacherServiceEntity;
import c.x.jy.service.service.TeacherServiceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("teacherServiceService")
public class TeacherServiceServiceImpl extends ServiceImpl<TeacherServiceDao, TeacherServiceEntity> implements TeacherServiceService {
	@Autowired
	private TeacherServiceService teacherServiceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<TeacherServiceEntity> query= 	 new QueryWrapper<TeacherServiceEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<TeacherServiceEntity> page = this.page(
                new Query<TeacherServiceEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

	@Override
	public List<TeacherServiceEntity> selectAllListBymemberId(Long memberId) {
		QueryWrapper<TeacherServiceEntity> query=new QueryWrapper<>();
		query.lambda().eq(TeacherServiceEntity::getMemberId, memberId);
		return this.getBaseMapper().selectList(query );
	}

	@Override
	public List<ServiceEntityDto> selectServiceBymemberId(Long memberId) {
		return this.getBaseMapper().selectServiceBymemberId(memberId);
	}

	@Override
	public List<ServiceEntityDto> selectServiceByTeacherId(Long teacherId) {
		return this.getBaseMapper().selectServiceByTeacherId(teacherId);
	}

	@Override
	public ServiceEntityDto selectServiceInfoById(Long id) {
		return this.getBaseMapper().selectServiceInfoById(id);
	}

	@Override
	public List<TeacherServiceEntity> getByTeacherId(Long id) {
		QueryWrapper<TeacherServiceEntity> query=new QueryWrapper<>();
		query.lambda().eq(TeacherServiceEntity::getTeacherId, id);
		return this.getBaseMapper().selectList(query);

	}
	@Transactional
	@Override
	public int updateServiceList(List<TeacherServiceEntity> services) {
    	if(services==null||services.isEmpty())throw new RRException("服务列表为空");
		QueryWrapper<TeacherServiceEntity> query=new QueryWrapper<>();
		query.lambda().eq(TeacherServiceEntity::getTeacherId,services.get(0).getTeacherId());
		this.getBaseMapper().delete(query);

		return this.getBaseMapper().insertServiceList(services);
	}

	@Override
	public TeacherServiceEntity selectTezcherServiceForUpdate(Long itemId) {
		// TODO Auto-generated method stub
		return this.getBaseMapper().selectTezcherServiceForUpdate(itemId);
	}

	@Override
	public PageUtils listforweb(Map<String, Object> params) {
		QueryWrapper<TeacherServiceEntity> query= 	 new QueryWrapper<TeacherServiceEntity>() ;
		if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
			query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
		if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
		IPage<TeacherServiceEntityDto> page = this.getBaseMapper().listforweb(
				new Query<TeacherServiceEntity>().getPage(params),
				query
		);

		return new PageUtils(page);
	}

	@Override
	public String getTeacherMiniPrice(Long teacherId) {
		List<TeacherServiceEntity> list = teacherServiceService.getByTeacherId(teacherId);
		TeacherServiceEntity teacherServiceEntity;
		BigDecimal miniPrice = new BigDecimal(0);
		if(list!=null&&list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				teacherServiceEntity = list.get(i);
				if(miniPrice.compareTo(new BigDecimal(0))==0)
					miniPrice = teacherServiceEntity.getDiscountPrice();
				else{
					if(miniPrice.compareTo(teacherServiceEntity.getDiscountPrice())==1){
						miniPrice=teacherServiceEntity.getDiscountPrice();
					}
				}
			}
		}else{
			return "--";
		}

		return miniPrice.toString();
	}
}
